﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SofireCodeGenerator.Core;
using System.IO;

namespace SofireCodeGenerator.UI
{
    public partial class EntityGeneratorForm : Form
    {
        public EntityGeneratorForm()
        {
            InitializeComponent();
            this.Icon = MainForm.FormIcon;
            this.ShowInTaskbar = false;
            this.StartPosition = FormStartPosition.CenterParent;
            VSEx.FillFieldTypeItems(this.col_FieldType);
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
            dataGridView1.AutoGenerateColumns = false;
        }

        private readonly SqlGeneratorBase Generator;
        private readonly string TableName;

        private DataTable ColumnsTable;

        public EntityGeneratorForm(SqlGeneratorBase generator, string tableName)
            : this()
        {
            this.Generator = generator;
            this.TableName = tableName;

            if(VSEx.ColumnsCache.TryGetValue(tableName, out this.ColumnsTable))
            {
                dataGridView1.DataSource = this.ColumnsTable;
            }
            else
            {
                this.tsb_Refresh_Click(tsb_Refresh, EventArgs.Empty);
            }
            this.Text = this.Text + "   -   " + this.TableName;
            txt_Name.Text = VSEx.GetAliasName(tableName) + "Entity";
            txt_OutputDirectory.Text = GlobalParameters.OutputDirectory;
            txt_XSqlDirectory.Text = Path.Combine(GlobalParameters.OutputDirectory, GlobalSetting.XSqlDirectory);
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            if(dataGridView1.IsCurrentCellInEditMode) dataGridView1.EndEdit();
        }

        private void tsb_Refresh_Click(object sender, EventArgs e)
        {
            var r = VSEx.RefreshColumns(this.Generator, this.TableName);
            if(this.IsSuccess(r))
            {
                this.ColumnsTable = r.Value;
                dataGridView1.DataSource = this.ColumnsTable;
            }

            //dataGridView1.AutoResizeColumns();
        }

        private void dataGridView1_ColumnHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            var column = dataGridView1.Columns[e.ColumnIndex];
            if(column.DataPropertyName == "IsSelected")
            {
                if(dataGridView1.IsCurrentCellInEditMode) dataGridView1.EndEdit();
                var isChecked = e.Button == System.Windows.Forms.MouseButtons.Left;
                foreach(DataGridViewRow row in dataGridView1.Rows)
                {
                    row.Cells[e.ColumnIndex].Value = isChecked;
                }
                dataGridView1.Refresh();
            }
        }

        private void btn_Builder_Click(object sender, EventArgs e)
        {

        }

        private void btn_Preview_Click(object sender, EventArgs e)
        {
            if(dataGridView1.IsCurrentCellInEditMode) dataGridView1.EndEdit();

            EntityGenerator gen = new EntityGenerator()
            {
                AllColumns = false,
                ClassName = txt_Name.Text,
                ColumnsTable = this.ColumnsTable,
                Generator = this.Generator,
                TableName = this.TableName
            };

            string entityCode, xsqlCode;
            gen.Create(out entityCode, out xsqlCode);
            new PreviewForm(this.TableName,entityCode, xsqlCode).ShowDialog();
            /*
             * TableComment
             * ClassName
             * Namespace
             * ConstFieldTemplates
             *      |- PropertyName
             *      |- FieldName
             *      |- Comment
             * PropertyTemplates
             *      |- PropertyType
             *      |- PropertyName
             *      |- Comment
             * EntityLoadableItemTemplates
             *      |- ClassName
             *      |- PropertyName
             *      |- PropertyFullType
             */
        }
    }
}
